﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Taula = DeltaFlex.Negoci.ConsTaules.Albara;
using System.Globalization;

namespace DeltaFlex.Negoci
{
    [Serializable()]
    public class Albara
    {
        #region Variables
        private int _idAlbara;
        private Ordre_Treball _ordreTreball;
        private Client _client;
        private DateTime _data;
        private decimal _importTotal;
        #endregion

        #region Atributs
        public int IdAlbara
        {
            get { return _idAlbara; }
            set { _idAlbara = value; }
        }

        public Ordre_Treball OrdreTreball
        {
            get { return _ordreTreball; }
            set { _ordreTreball = value; }
        }

       public Client Client
        {
            get { return _client; }
            set { _client = value; }
        }
                
        public DateTime Data
        {
            get { return _data; }
            set { _data = value; }
        }

        public decimal ImportTotal
        {
            get { return _importTotal; }
            set { _importTotal = value; }
        }

        #region Bindings
        public int BindOrdreTreball
        {
            get { return _ordreTreball.IdOrdre_Treball; }
        }

        public string BindData
        {
            get { return _data.ToShortDateString(); }
        }

        public string BindClient
        {
            get { return _client.NomClient; }
        }

        public string BindImportTotal
        {
            get
            {
                return _importTotal.ToString("C2", CultureInfo.CurrentCulture); 
            }
        }
        #endregion

        #endregion

        #region Inserts
        public static int InsertarAlbara(int idAlbara, int idOrdreTreball, int idClient, decimal importTotal)
        {
            try
            {
                return  DeltaFlex.Data.Albara.InsertarAlbara(idAlbara, idOrdreTreball, idClient, importTotal);
            }
            catch { throw; }
        }

        #endregion

        #region Update
        public static void ActualitzarAlbara(int idAlbara, int idOrdre, string pedidoClient, string referenciaClient, string idTreball,
                                             string numTroquel, int idClient, DateTime data, decimal total)
        {
            try
            {
                DeltaFlex.Data.Albara.ActualitzarAlbara(idAlbara, idOrdre, pedidoClient,referenciaClient,idTreball,
                                                        numTroquel, idClient, data, total);
            }
            catch { throw; }
        }
        #endregion

        #region Select
        /// <summary>
        /// Retorna el següent index d'un albarà
        /// </summary>
        /// <returns></returns>
        public static int Cercar_NextIdAlbara()
        {
            int nextIdAlbara = -1;
            IDataReader reader = null;

            try
            {
                reader = DeltaFlex.Data.Albara.Cercar_NextIdAlbara();

                if (reader.Read())
                    nextIdAlbara = MapEntityFromDataReader(reader)._idAlbara;
            }
            catch (Exception) { throw; }
            finally
            {
                if (reader != null) reader.Close();
            }
            return nextIdAlbara;
        }

        public static List<Albara> LlistarAlbarans(int idClient, int? idOrdre, DateTime? dataInici, DateTime? dataFi)
        {
            List<Albara> lstAlbara = new List<Albara>();
            IDataReader reader = null;

            try
            {
                reader = DeltaFlex.Data.Albara.LlistarAlbarans(idClient, idOrdre, dataInici, dataFi);

                while(reader.Read())
                    lstAlbara.Add(MapEntityFromDataReader(reader));
            }
            catch { throw; }
            finally
            {
                if (reader != null) reader.Close();
            }

            return lstAlbara;
        }

        public static Albara LlistarAlbaraByIdAlbara(int idAlbara)
        {
            Albara albara = new Albara();
            IDataReader reader = null;

            try
            {
                reader = DeltaFlex.Data.Albara.LlistarAlbaraByIdAlbara(idAlbara);
                if (reader.Read())
                    albara = MapEntityFromDataReader(reader);
            }
            
            catch (Exception) { throw; }
            finally
            {
                if (reader != null) reader.Close();
            }
            return albara;
        }

        #endregion

        #region Mapeig
        public static Albara MapEntityFromDataReader(IDataReader record)
        {
            Albara albara = new Albara();
            MemberAllocation.Allocate(record, Taula.COLUMNA_IDALBARA, ref albara._idAlbara);
            
            albara._ordreTreball = Ordre_Treball.MapEntityFromId(record);
            
            albara._client = Client.MapEntityFromId(record);

            MemberAllocation.Allocate(record, Taula.COLUMNA_DATA, ref albara._data);
            MemberAllocation.Allocate(record, Taula.COLUMNA_TOTAL, ref albara._importTotal);
                       
            return albara;
        }

        public static Albara MapEntityFromId(IDataReader record)
        {
            Albara albara = new Albara();
            MemberAllocation.Allocate(record, Taula.COLUMNA_IDALBARA, ref albara._idAlbara);
            return LlistarAlbaraByIdAlbara(albara._idAlbara);
        }

        #endregion

    }
}




